// pages/orders/orders.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    list: [],
    page: 1,
    _source: {
      'cgj': { name: '窗管家', icon: '../../assets/cgj.png' }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => { // 用这种方法调用，this指向Page
        this.setData({
          winH: res.windowHeight
        });
      }
    })
  },
  handleInput(e) {
    let _arr = []
    
    console.log(e.detail.value)
    if (e.detail.value !== "") {
      this.setData({
        list: [],
        page: 1,
        value: e.detail.value
      })
      this.getClues(_arr, e.detail.value)
    }
  },
  handleOrder(e) {
    console.log(e.currentTarget.dataset.order)
    wx.navigateTo({
      url: '../../ordersNew/ordersNew?title=' + JSON.stringify(e.currentTarget.dataset.order),
    })
  },
  handleEdit(e) {
    console.log(e)
    wx.navigateTo({
      url: '../../cluesNew/cluesNew?title=' + JSON.stringify(e.currentTarget.dataset.order) + '&num=' + e.currentTarget.dataset.index,
    })
  },
  handleEnd(e) {

    // return
    app.query('/api/clues/' + e.currentTarget.dataset.end, {}, 'delete').then(res => {
      if (res.status === 'success') {
        // ap
        app.showToast('关闭成功', 'success', 500)
        this.setData({
          list: [],
          page: 1
        })
        this.getClues([] ,this.data.value)
      } else {
        app.showToast(res.msg, false, 500, '../../../assets/warning.png')
      }
    })
  },  
  handleConfirm() {

  },
  scroll(e) {
    // console.log(e)
  },
  lower: function (e) {
    console.log(e)
    const { list } = this.data;
    let _list = list;
    let _date = new Date();
    // let _data = []
    if (_list % 10 === 0) {
      this.getOrder(_list)
    } else {
      app.showToast('暂无更多数据', false, 500, '../../../assets/warning.png')
    }
    // this.setData({ list: _list, visibled: false })
  },
  quxiao() {
    wx.navigateBack({
      delta: 1
    })
  },
  getClues(e, key) {
    const { list, page, loading, _tab } = this.data

    let _list = list;
    let _remark, _num;

    let _data = {
      'null': { page: page },
      'assign': { page: page, type: _tab }
    }
    if (loading) {
      return
    }
    this.setData({ loading: true })
    app.query('/api/clues', { page: page, q: key }, 'get').then(res => {
      if (res.status == 'success') {
        // wx.setStorageSync('aaa', res)
        for (let i = 0; i < res.list.length; i++) {
          if (res.list[i].remark !== 'null' && !!res.list[i].remark) {
            _remark = res.list[i].remark.split('~');
            _remark = _remark[_remark.length - 1]
            res.list[i].remark = JSON.parse(_remark)
            // console.log(JSON.parse(_remark))
          }
        }
        this.setData({
          list: _list.concat(res.list),
          page: page + 1,
          loading: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})